package com.backend.demo.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.backend.demo.entities.Role;
import com.backend.demo.entities.Users;
import com.backend.demo.service.IMenuService;
import com.backend.demo.service.IRoleService;
import com.backend.demo.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
 
/**
 * 用户管理
 * 
 * @author Administrator
 * 
 */
@ParentPackage("user")
@Namespace("/jspView")
@InterceptorRefs(value = { @InterceptorRef(value = "loginStack") })
@Controller
public class UserAction extends ActionSupport implements SessionAware,RequestAware,ModelDriven<Users>{

	private static final long serialVersionUID = 2978033288684073999L;

	private Map<String,Object> session;
	
	private Map<String,Object> request;
	
	private Users user;
	
	private String result;
	
	//修改后的密码
	private String resetPassword;
	
	@Resource(name = "userService")
	private IUserService userService;
	
	@Resource(name="roleService")
	private IRoleService roleService;
	
	@Resource(name="menuService")
	private IMenuService menuService;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	public String getResetPassword() {
		return resetPassword;
	}

	public void setResetPassword(String resetPassword) {
		this.resetPassword = resetPassword;
	}

	@Override
	public Users getModel() {
		if(user == null){
			user = new Users();
		}
		return user;
	}
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}
	
	public String getResult() {
		return result;
	}
	
	public void setResult(String result) {
		this.result = result;
	}

	@Action(value="login",results={@Result(name=SUCCESS,location="/jspView/index.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String login() {
		Users users = userService.userLogin(user.getUserName(), user.getUserPassword());
		String language = ServletActionContext.getRequest().getParameter("language");
		if(StringUtils.isEmpty(language)){
			return ERROR;
		}
		if("用户名".equals(language)){
			language = "zh_CN";
		}else{
			language = "en_US";
		}
		if(users != null){
			long roleID = users.getRoleID();
			Role role = roleService.getRoleById(roleID);
			session.put("roleName", role.getRoleName());
			session.put("user", users);
			session.put("language", language);
			session.put("menus", menuService.getParentMenus());
			session.put("subMenus", menuService.menusMap());
		}
		return users == null ? ERROR:SUCCESS;
	}
	
	@Action(value="logout",results={@Result(name=SUCCESS,location="/jspView/login.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String logout(){
		int num = userService.logout("1", user.getUserID());
		if(num == 1){
			return SUCCESS;
		}
		return  ERROR;
	}
	
	@Action(value="addUser",results={@Result(name=SUCCESS,location="/jspView/getAllUsers",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String addUser(){
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		int num = userService.addUser(user);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="getAllUsers",results={@Result(name=SUCCESS,location="/jspView/users/userList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getAllUsers(){
		
		List<Users> users = userService.getAllUsers();
		if(users != null && users.size() > 0){
			request.put("users", users);
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="editUserMsg",results={@Result(name=SUCCESS,location="/jspView/getAllUsers",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String editUserMsg(){
		int num = userService.editUserMsg(user);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Actions({
		@Action(value="getUserById",results={@Result(name=SUCCESS,location="/jspView/users/editUser.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")}),
		@Action(value="getPassword",results={@Result(name=SUCCESS,location="/jspView/users/resetUserPassword.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")}),
		@Action(value="viewUser",results={@Result(name=SUCCESS,location="/jspView/users/viewUser.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	})
	public String getUserById(){
		Users users = userService.getUsersById(user.getUserID());
		if(users != null){
			request.put("user", users);
			request.put("profileimgFileName", users.getImageFile());
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="deleteUser",results={@Result(name=SUCCESS,location="/jspView/getAllUsers",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String deleteUser(){
		int num = userService.deleteById(user.getUserID());
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}
	
	@Action(value="getUsersByCondition",results={@Result(name=SUCCESS,location="/jspView/users/userList.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String getUsersByCondition(){
		List<Users> users = userService.getAllUsersByCondition(user);
		request.put("users", users);
		return SUCCESS;
	}
	
	@Action(value="resetPassword",results={@Result(name=SUCCESS,location="/jspView/getAllUsers",type="redirect"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String resetPassword(){
		int num = userService.resetPassword(user.getUserName(), resetPassword);
		if(num == 1){
			return SUCCESS;
		}
		return ERROR;
	}

	@Action(value="redirectPage",results={@Result(name=SUCCESS,location="/jspView/users/addUser.jsp"),@Result(name=ERROR,location="/jspView/login.jsp")})
	public String redirectPage(){
		String language = (String) session.get("language");
		List<Role> roles = roleService.getAllRoles();
		if(roles != null && roles.size() > 0){
			request.put("roles", roles);
			request.put("language", language);
			return SUCCESS;
		}
		return ERROR;
	}
}
